#!/bin/bash

has_tianni() {
    # mount can failed silently
    [[ -z "$TIANNI_SRC" || -z "$TIANNI_DIR" ]] || mount $TIANNI_SRC $TIANNI_DIR &>/dev/null
    cat ${TIANNI_DIR}/VERSION |grep "^TianNi" &>/dev/null
}

install_kolla() {
    local upgrade=(pip setuptools)
    local install=(docker ansible kolla-ansible tox)

    has_tianni
    if [ $? -eq 0 ]; then
        pip install --upgrade --no-index --find-links ${TIANNI_DIR}/pip/ ${upgrade[@]} 
        pip install --no-index --find-links ${TIANNI_DIR}/pip/ pbr
        pip install --no-index --find-links ${TIANNI_DIR}/pip/ ${install[@]}
    else
        pip install --upgrade ${upgrade[@]} -i https://mirrors.aliyun.com/pypi/simple/
        pip install ${install[@]} -i https://mirrors.aliyun.com/pypi/simple/
    fi

    mkdir -p /etc/ansible
    cat > /etc/ansible/ansible.cfg << eof1
[defaults]
pipelining=True
forks=100
deprecation_warnings=False
eof1
}

config_kolla() {
    mkdir -p $ETCKOLLA
    cp -r ${KOLLADIR}/etc_examples/kolla/* $ETCKOLLA
    cp ${KOLLADIR}/ansible/inventory/* $ETCKOLLA
}

ansible_ping_hosts() {
    ansible -i $ETCKOLLA/$INVENTORY all -m ping
}


cmdopts=$(getopt --longoptions skip-install-kolla,genpwd,confdir:\
                 --options +kd:  -- "$@")
if [ $? -ne 0 ] ; then
  echo "Terminating..." 1>&2
  exit 1
fi

# set positional parameters
eval set -- "$cmdopts"

skip_instlal_kolla=false
gen_pwd=false
confdir=.

while true; do
  case "$1" in
    k | --skip-install-kolla )
        skip_instlal_kolla=true
        shift ;;
    -d | --confdir )
        confdir="$2"
        shift 2;;
    --genpwd )
        gen_pwd=true
        shift ;;
    -- ) shift; break ;;
    * ) break ;;
  esac
done

# test and skip if defined
[ -z "$KOLLADIR" ] && { echo "source tn-openstack-var"; . tn-openstack-var; }

# check on host
type pip || { echo "pip not found" >&1; exit 1; }
type easy_install || { echo "please install python setuptools" >&1; exit 1; }

# copy files to work dir, then activate venv
set -o xtrace
mkdir -p $WORKDIR
pwdfile="$(cd "$(dirname $confdir/passwords.yml)";pwd)/$(basename $confdir/passwords.yml)"

# workaround: set default values to avoid extras.yml failed
grep "^enable_neutron_taas"  $confdir/globals.yml || echo "enable_neutron_taas: \"no\"" >> $confdir/globals.yml
grep "^enable_cinder_backend_iscsi" $confdir/globals.yml || echo "enable_cinder_backend_iscsi: \"yes\"" >> $confdir/globals.yml

cp -r $confdir/globals.yml \
 $confdir/$INVENTORY \
 extras.yml \
 extras.var \
 files/ \
 $WORKDIR || exit -1
cd $WORKDIR
virtualenv $VENVDIR || exit -1
source ${VENVDIR}/bin/activate
set +o xtrace

# install kolla in venv
if $skip_instlal_kolla; then
    :
else
    install_kolla || exit -2
fi

# check kolla verison
if [ "$(pip show kolla-ansible |grep "^Version" |awk '{print $2}')" != "$KOLLAVER" ] ;then
  echo "ERROR: kolla-ansible version mismatch! $KOLLAVER not found" >&1
  exit -2
fi

# copy kolla config
config_kolla || exit -3

if $gen_pwd; then
    echo "kolla-genpwd -p $pwdfile"
    cp ${KOLLADIR}/etc_examples/kolla/passwords.yml $pwdfile
    kolla-genpwd -p $pwdfile || exit -5
    echo "please set docker_registry_password"
    sleep 3
    vim $pwdfile
fi

cp globals.yml $INVENTORY $pwdfile extras.var $ETCKOLLA || exit -1

ansible_ping_hosts || exit -4
